package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.pojo.Chapter;
import com.tpyule.tpclass.entity.pojo.Section;
import com.tpyule.tpclass.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/3/29 21:00
 */
@RestController
@RequestMapping("/section")
public class SectionController extends BaseController{
    @Autowired
    SectionService sectionService;

    @RequestMapping("/addSection")
    @ResponseBody
    public void addSection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String sectionData = request.getParameter("sectionData");
            Section section = objectMapper.readValue(sectionData,Section.class);
            Section result = sectionService.addSection(section);
            if(request==null){
                resultBean = new FailResultBean("添加失败");
            }else {
                resultBean = new SuccessResultBean<Section>(result,"添加成功");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/updateSection")
    @ResponseBody
    public void getListSection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String sectionData = request.getParameter("sectionData");
            Section section = objectMapper.readValue(sectionData,Section.class);
            String result = sectionService.updateSection(section);
            if("error".equals(result)){
                resultBean = new FailResultBean("更新失败");
            }else {
                resultBean = new SuccessResultBean<Section>(section,"更新成功");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getSection")
    @ResponseBody
    public void getSection(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String sectionId = request.getParameter("sectionId");
            if(sectionId!=null){
                Section section = sectionService.getSection(sectionId);
                if(section!=null)
                    resultBean = new SuccessResultBean<Section>(section,"更新成功");
                else
                    resultBean = new FailResultBean("该节不存在");
            }else {
                resultBean = new FailResultBean("节ID异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getIndexSectionId")
    @ResponseBody
    public void getIndexSectionId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String courseId = request.getParameter("courseId");
            if(courseId!=null){
                Section section = sectionService.getIndexSection(courseId);
                if(section!=null)
                    resultBean = new SuccessResultBean<Section>(section,"获取成功");
                else
                    resultBean = new FailResultBean("获取失败");
            }else {
                resultBean = new FailResultBean("课程ID异常");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
